package com.supplychainapi.controller;

/**
 * Created by shenhong on 2017/10/18.
 */

import com.joindata.inf.common.basic.exceptions.ServiceException;
import com.joindata.inf.common.util.basic.JsonUtil;
import com.supplychainapi.common.ApiVersion;
import com.supplychainapi.common.BizCode;
import com.supplychainapi.common.RestResponse;
import com.supplychainapi.domain.LoginCache;
import com.supplychainapi.model.ScmTbcust;
import com.supplychainapi.model.ScmTbcustExtend;
import com.supplychainapi.model.ScmTbtreebuild;
import com.supplychainapi.service.IScmTbtreebuildService;
import com.supplychainapi.service.phone.cust.IScmTbcustPhoneService;
import com.supplychainapi.utils.Constants;
import com.supplychainapi.utils.PathUitls;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 *
 * 手机版客户
 * @author:shenhong
 * @email:shenhong@niwodai.net
 * @date:2017-10-12
 *
 */

@RequestMapping(PathUitls.ROOT_PHONE + PathUitls.ROOT_VERSION+PathUitls.ROOT_Cust)
@RestController
@Api(value = "CustPhoneController", description = "手机版客户")
public class CustPhoneController extends BaseController{

    private Logger logger = LoggerFactory.getLogger(SupplierController.class);

    @Autowired
    private IScmTbcustPhoneService scmTbcustPhoneService;

    //树结构
    @Autowired
    private IScmTbtreebuildService scmTbtreebuildService;

    @ApiVersion(1)
    @ApiOperation("新增客户手机版")
    @RequestMapping(value = "/addScmTbcustPhoneInfo", method=RequestMethod.POST)
    public RestResponse<ScmTbcust>  addScmTbcustPhoneInfo(@RequestBody ScmTbcust cust, @ApiIgnore LoginCache loginCache, HttpServletRequest request){

        logger.info("参数信息 ----- cust={}", cust == null ? null : JsonUtil.toJSON(cust, false));

        if (StringUtils.isEmpty(cust.getFsCustId())) {
            return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.PARAM_NULL.getNum(), "客户代码不能为空");
        }

        if (StringUtils.isEmpty(cust.getFsCustName())) {
            return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.PARAM_NULL.getNum(), "客户名称不能为空");
        }

        if (StringUtils.isEmpty(cust.getFsNodeCode())) {

            return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.PARAM_NULL.getNum(), "客户分类ID不能为空");
        }

		/*if(StringUtils.isEmpty(supplier.getFsNodeText())){
            return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.PARAM_NULL.getNum(), "树节点名称不能为空");
		}*/

        if (StringUtils.isEmpty(cust.getFsNodeFullText())) {
            return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.PARAM_NULL.getNum(), "树节点内容全名不能为空");
        }
        cust.setFsShopGUID(loginCache.getFsShopGUID());
        cust.setFsCreateUserId(loginCache.getFsUserId());
        cust.setFsCreateUserName(loginCache.getFsUserName());
        cust.setFsUpdateUserId(loginCache.getFsUserId());
        cust.setFsUpdateUserName(loginCache.getFsUserName());
        try {
            Boolean bl = scmTbcustPhoneService.getCustNameIsExit(loginCache.getFsShopGUID(), cust.getFsCustName(), cust.getFsCustId(), "add");
            if (bl) {
                return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.Cust_ERROR_MESSAGE.getNum(), "当前客户名称已存在");
            }
            scmTbcustPhoneService.addScmTbcustPhoneInfo(cust);
        } catch (ServiceException e) {
            e.printStackTrace();
            return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), "新增客户异常");
        }

        return RestResponse.success();
    }
    @ApiVersion(1)
    @ApiOperation("更新客户手机版")
    @RequestMapping(value = "/updateScmTbcustPhoneInfo", method=RequestMethod.POST)
    public RestResponse<ScmTbcust>  updateScmTbcustPhoneInfo(@RequestBody ScmTbcust cust,@ApiIgnore LoginCache loginCache, HttpServletRequest request){

        logger.info("参数信息 ----- orderInVo={}", cust == null ? null : JsonUtil.toJSON(cust,false));

        if (cust.getFsCustId() == null) {
            return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.PARAM_NULL.getNum(), "供应商代码不能为空");
        }

        if (cust.getFsCustName() == null) {
            return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.PARAM_NULL.getNum(), "供应商名称不能为空");
        }
        cust.setFsShopGUID(loginCache.getFsShopGUID());
        cust.setFsUpdateUserId(loginCache.getFsUserId());
        cust.setFsUpdateUserName(loginCache.getFsUserName());
        cust.setFsUpdateTime(new Date());
        try {
            Boolean bl = scmTbcustPhoneService.getCustNameIsExit(loginCache.getFsShopGUID(), cust.getFsCustName(), cust.getFsCustId(), "update");
            if (bl) {
                return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.SUPPLIER_ERROR_MESSAGE.getNum(), "当前供应商名称已存在");
            }
            scmTbcustPhoneService.updateScmTbcustPhoneInfo(cust);
        } catch (ServiceException e) {
            e.printStackTrace();
            return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), e.getMessage());
        }

        return RestResponse.success();
        /*   logger.info("参数信息 ----- orderInVo={}", cust == null ? null : JsonUtil.toJSON(cust,false));
        cust.setFsShopGUID(loginCache.getFsShopGUID());
        if(cust.getFsCustId()==null){
            return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.PARAM_NULL.getNum(), "客户代码不能为空");
        }

        if(cust.getFsCustName()==null){
            return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.PARAM_NULL.getNum(), "客户名称不能为空");
        }

        try {
            scmTbcustPhoneService.updateScmTbcustPhoneInfo(cust);
        } catch (ServiceException e) {
            e.printStackTrace();
            return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), "更新客户异常");
        }

        return RestResponse.success();*/
    }
    @ApiVersion(1)
    @ApiOperation("获取客户手机版分类树结构信息")
    @RequestMapping(value = "/getScmTbtreebuildPhoneAllInfo", method=RequestMethod.GET)
    public  RestResponse<List<ScmTbtreebuild>>  getScmTbtreebuildPhoneAllInfo(@ApiIgnore LoginCache loginCache){

        logger.info("fsShopGUID = {}",loginCache.getFsShopGUID());
        List<ScmTbtreebuild> resulScmTbtreebuildtList=null;
        try {
            resulScmTbtreebuildtList = scmTbtreebuildService.getScmTbtreebuildAllInfo(loginCache.getFsShopGUID(),"Cust");
        } catch (ServiceException e) {
            e.printStackTrace();
            return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), "获取客户ß分类树结构信息访问异常");
        }
        return  RestResponse.success(resulScmTbtreebuildtList);
    }
    @ApiVersion(1)
    @ApiOperation("根据客户名称获取客户信息")
    @RequestMapping(value = "/getCustPhoneInfo", method=RequestMethod.GET)
    public  RestResponse<ScmTbcust>  getCustInfo(@ApiParam(value = "客户名称", required = true)@RequestParam String fsCustId){
        logger.info("----fsCustId=----"+fsCustId);
        ScmTbcustExtend custDetails = null;
        try {
            custDetails = scmTbcustPhoneService.getCustPhoneInfo(fsCustId);
        } catch (ServiceException e) {
            e.printStackTrace();
            return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), e.getMessage());
        }
        return RestResponse.success(custDetails);
    }
    @ApiVersion(1)
    @ApiOperation("查询客户信息")
    @RequestMapping(value = "/getCustByFsShopGUIDPhoneList", method=RequestMethod.GET)
    public  RestResponse<ScmTbcust>  getCustByFsShopGUIDList(@ApiIgnore LoginCache loginCache,@ApiParam(value = "客户id", required = true)@RequestParam String fsCustId){
        logger.info("fsShopGUID={},fsCustId = {}",loginCache.getFsShopGUID(),fsCustId);
        ScmTbcust resulScmTbcust=null;
        try {
            resulScmTbcust = scmTbcustPhoneService.getCustByFsShopGUIDPhoneList(loginCache.getFsShopGUID(),fsCustId);
        } catch (ServiceException e) {
            e.printStackTrace();
            return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), "查询供应商信息访问异常");
        }
        return  RestResponse.success(resulScmTbcust);
    }



    @ApiVersion(1)
    @ApiOperation("删除客户")
    @RequestMapping(value = "/deleteCustPhoneInfo", method=RequestMethod.GET)
    public RestResponse<ScmTbcust>  deleteCustInfo(@ApiParam(value = "客户代码", required = true)@RequestParam String fsCustId,@ApiIgnore LoginCache loginCache,HttpServletRequest request){

        logger.info("参数信息 ----- fsCustId={},fsShopGUID={}", fsCustId,loginCache.getFsShopGUID());

        if(fsCustId==null || fsCustId.trim().equals("")){
            return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.PARAM_NULL.getNum(), "客户代码不能为空");
        }

        if(loginCache.getFsShopGUID()==null || loginCache.getFsShopGUID().trim().equals("")){
            return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.PARAM_NULL.getNum(), "门店编号不能为空");
        }
        String[] arrFsCustId = fsCustId.split(",");
        List<String> fsCustIdList = java.util.Arrays.asList(arrFsCustId);
        String[] arrFsShopGUID= loginCache.getFsShopGUID().split(",");
        List<String> fsShopGUIDList = java.util.Arrays.asList(arrFsShopGUID);
        try {
            scmTbcustPhoneService.deleteCustPhoneInfo(fsCustIdList,fsShopGUIDList);
        } catch (ServiceException e) {
            e.printStackTrace();
            return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), "删除供应商异常");
        }

        return RestResponse.success();
    }

    @ApiVersion(1)
    @ApiOperation("查询客户信息")
    @RequestMapping(value = "/getByKeyWordCustInfoPhoneList", method=RequestMethod.GET)
    public  RestResponse<List<ScmTbcust>>  getByKeyWordCustInfoList(@ApiParam(value = "客户代码 客户名称 助记码", required = true)@RequestParam String keyWord,@ApiParam(value = "页码", required = true)@RequestParam Integer page,@ApiParam(value = "数据大小", required = true)@RequestParam Integer pageSize){
        logger.info("keyWord = {},page={},pageSize={}",keyWord,page,pageSize);
        List<ScmTbcust> resulScmTbcust=null;
        try {
            resulScmTbcust = scmTbcustPhoneService.getByKeyWordCustInfoPhoneList(keyWord, page, pageSize);
        } catch (ServiceException e) {
            e.printStackTrace();
            return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), "查询关键字客户信息访问异常");
        }
        return  RestResponse.success(resulScmTbcust);
    }

    @ApiVersion(1)
    @ApiOperation("获取客户手机版列表信息")
    @RequestMapping(value = "/getCustPhoneListInfo", method=RequestMethod.GET)
    public  RestResponse<List<ScmTbcust>>  getCustPhoneListInfo(@ApiIgnore LoginCache loginCache,@ApiParam(value = "分类代码", required = true)@RequestParam String fsNodeCode,@ApiParam(value = "页码", required = true)@RequestParam Integer page,@ApiParam(value = "显示条数", required = true)@RequestParam Integer pageSize){
        logger.info("fsShopGUID={},fsNodeCode={},page={},pageSize={}",loginCache.getFsShopGUID(),fsNodeCode,page,pageSize);
        List<ScmTbcust> custList = null;
        try {
            custList = scmTbcustPhoneService.getCustPhoneListInfo(loginCache.getFsShopGUID(),fsNodeCode,page,pageSize);
        } catch (ServiceException e) {
            e.printStackTrace();
            return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), "查询供应商列表异常");
        }
        return RestResponse.success(custList);
    }

    @ApiVersion(1)
    @ApiOperation("根据客户名称模糊查询客户手机版列表信息")
    @RequestMapping(value = "/getCustPhoneByFsCustNameListInfo", method=RequestMethod.GET)
    public  RestResponse<List<ScmTbcust>>  getCustPhoneByFsCustNameListInfo(@ApiIgnore LoginCache loginCache,@ApiParam(value = "客户名称", required = true)@RequestParam String fsCustName,@ApiParam(value = "页码", required = true)@RequestParam Integer page,@ApiParam(value = "显示条数", required = true)@RequestParam Integer pageSize){
        logger.info("fsShopGUID={},fsCustName={},page={},pageSize={}",loginCache.getFsShopGUID(),fsCustName,page,pageSize);
        List<ScmTbcust> custByFsCustNameList = null;
        try {
            custByFsCustNameList = scmTbcustPhoneService.getCustPhoneByFsCustNameListInfo(loginCache.getFsShopGUID(),fsCustName,page,pageSize);
        } catch (ServiceException e) {
            e.printStackTrace();
            return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), "根据供应商名称查询供应商列表异常");
        }
        return RestResponse.success(custByFsCustNameList);
    }

    @ApiVersion(1)
    @ApiOperation(value="根据父节点查询供应商分类子信息", notes="")
    @RequestMapping(value = "/getPhoneCustClass", method=RequestMethod.GET)
    public RestResponse<List<ScmTbtreebuild>> getPhoneCustClass( @ApiIgnore LoginCache loginCache,@ApiParam(value = "父节点", required = true)@RequestParam String fdParentNodeCode, HttpServletRequest request) throws ServiceException {
        logger.info("fdParentNodeCode = {}",fdParentNodeCode);
        return RestResponse.success(scmTbtreebuildService.getScmTbtreebuildAllInfos(loginCache.getFsShopGUID(), Constants.CUST_CLASS,fdParentNodeCode));
    }

    @ApiVersion(1)
    @ApiOperation("获取客户手机版列表全部信息")
    @RequestMapping(value = "/getCustAllPhoneListInfo", method=RequestMethod.GET)
    public RestResponse<List<ScmTbcustExtend>> getCustAllPhoneListInfo(@ApiIgnore LoginCache loginCache, @ApiParam(value = "页码", required = true) @RequestParam Integer page, @ApiParam(value = "显示条数", required = true) @RequestParam Integer pageSize) {
        logger.info("fsShopGUID={},page={},pageSize={}",loginCache.getFsShopGUID(),page,pageSize);
        List<ScmTbcustExtend> custsList = null;
        try {
            custsList = scmTbcustPhoneService.getCustAllPhoneListInfo(loginCache.getFsShopGUID(),page,pageSize);
        } catch (ServiceException e) {
            e.printStackTrace();
            return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), "查询客户列表异常");
        }
        return RestResponse.success(custsList);
    }

    @ApiVersion(1)
    @ApiOperation("获取客户手机版列表父节点以下的信息")
    @RequestMapping(value = "/getCustParentAllPhoneListInfo", method=RequestMethod.GET)
    public  RestResponse<List<ScmTbcust>>  getSupplierParentAllPhoneListInfo(@ApiIgnore LoginCache loginCache,@ApiParam(value = "分类代码", required = true)@RequestParam String fsNodeCode,@ApiParam(value = "页码", required = true)@RequestParam Integer page,@ApiParam(value = "显示条数", required = true)@RequestParam Integer pageSize){
        logger.info("fsShopGUID={},fsNodeCode={},page={},pageSize={}",loginCache.getFsShopGUID(),fsNodeCode,page,pageSize);
        List<ScmTbcust> custsList = null;
        try {
            custsList = scmTbcustPhoneService.getCustParentAllPhoneListInfo(loginCache.getFsShopGUID(),fsNodeCode,page,pageSize);
        } catch (ServiceException e) {
            e.printStackTrace();
            return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), "查询客户列表异常");
        }
        return RestResponse.success(custsList);
    }


    /**
     @ApiOperation("获取供应商手机版一级分类信息")
     @RequestMapping(value = "/getScmTbtreebuildOneLevelInfos", method=RequestMethod.GET)
     public  RestResponse<List<ScmTbtreebuild>>  getScmTbtreebuildOneLevelInfos(@ApiParam(value = "门店ID", required = true)@RequestParam String fsShopGUID,@ApiParam(value = "页码", required = true)@RequestParam Integer page,@ApiParam(value = "数据大小", required = true)@RequestParam Integer pageSize){
     logger.info("fsShopGUID = {}",fsShopGUID);
     List<ScmTbtreebuild> resulScmTbtreebuildtList=null;
     try {
     resulScmTbtreebuildtList = scmTbtreebuildService.getScmTbtreebuildOneLevelInfos(fsShopGUID,page,pageSize,"Supplier");
     } catch (ServiceException e) {
     e.printStackTrace();
     return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), "获取供应商一级分类信息访问异常");
     }
     return  RestResponse.success(resulScmTbtreebuildtList);
     }
     **/

   /* @ApiOperation("增加供应商分类手机版")
    @RequestMapping(value = "/addScmTbtreebuildPhoneInfo", method=RequestMethod.POST)
    public RestResponse<ScmTbtreebuild>  addScmTbtreebuildPhoneInfo(@RequestBody ScmTbtreebuildParam model,@ApiIgnore LoginCache loginCache, HttpServletRequest request){
        logger.info("参数信息 ----- model={}", model == null ? null : JsonUtil.toJSON(model,false));
        if(model==null){
            return RestResponse.fail(BizCode.FAIL.getNum(), "新增供应商分类参数不能为空");
        }
        model.setFsTreeTypeId("Supplier");
        try {
            boolean bl = scmTbtreebuildService.saveTreeBuild(model);
            if(!bl){
                return RestResponse.fail(BizCode.FAIL.getNum(), "新增供应商分类失败");
            }
        } catch (ServiceException e) {
            e.printStackTrace();
            return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), "增加供应商分类异常");
        }
        return RestResponse.success();
    }

    @ApiOperation("更新供应商分类手机版")
    @RequestMapping(value = "/updateScmTbtreebuildPhoneInfo", method=RequestMethod.POST)
    public RestResponse<ScmTbtreebuild>  updateScmTbtreebuildPhoneInfo(@RequestBody ScmTbtreebuildParam model,@ApiIgnore LoginCache loginCache, HttpServletRequest request){
        logger.info("参数信息 ----- model={}", model == null ? null : JsonUtil.toJSON(model,false));
        if(model==null){
            return RestResponse.fail(BizCode.FAIL.getNum(), "更新供应商分类参数不能为空");
        }
        try {
            boolean bl = scmTbtreebuildService.updateTreeBuild(model);
            if(!bl){
                return RestResponse.fail(BizCode.FAIL.getNum(), "更新供应商分类失败");
            }
        } catch (ServiceException e) {
            e.printStackTrace();
            return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), "更新供应商分类异常");
        }
        return RestResponse.success();
    }


    @ApiOperation("删除供应商分类手机版")
    @RequestMapping(value = "/deleteScmTbtreebuildPhoneInfo", method=RequestMethod.POST)
    public RestResponse<ScmTbtreebuild>  deleteScmTbtreebuildPhoneInfo(@RequestBody ScmTbtreebuildParam model, HttpServletRequest request){
        logger.info("参数信息 ----- model={}", model == null ? null : JsonUtil.toJSON(model,false));
        if(model==null){
            return RestResponse.fail(BizCode.FAIL.getNum(), "删除供应商分类参数不能为空");
        }
        try {
            boolean bl = scmTbtreebuildService.deleteTreeBuild(model);
            if(!bl){
                return RestResponse.fail(BizCode.FAIL.getNum(), "删除供应商分类失败");
            }
        } catch (ServiceException e) {
            e.printStackTrace();
            return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), "删除供应商分类异常");
        }
        return RestResponse.success();
    }

    @ApiOperation("查询供应商分类详情信息")
    @RequestMapping(value = "/getSupplierByPhoneFsSupplierNameInfo", method=RequestMethod.POST)
    public  RestResponse<ScmTbtreebuild>  getSupplierByPhoneFsSupplierNameInfo(@RequestBody ScmTbtreebuildParam model){
        logger.info("参数信息 ----- model={}", model == null ? null : JsonUtil.toJSON(model,false));
        if(model==null){
            return RestResponse.fail(BizCode.FAIL.getNum(),BizCode.PARAM_NULL.getNum(), "参数不能为空");
        }
        ScmTbtreebuild scmTbtreebuild=null;
        try {
            scmTbtreebuild = scmTbtreebuildService.selectByKey(model);
        } catch (ServiceException e) {
            e.printStackTrace();
            return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), "供应商分类详情访问异常");
        }
        return  RestResponse.success(scmTbtreebuild);
    }
*/
}

